from unihiker import GUI
from pinpong.board import Servo
from pinpong.board import Board, Pin
from pinpong.extension.unihiker import *

gui = GUI()

Board().begin()

rectangle_x = 120 
rectangle_y = 50
angle = 0
position_dict = {}
postition_xy = {}
x = 0
y = 0

user_text = gui.draw_text(x=120, y=108, text='按A键进入用户模式', origin='bottom')
root_text = gui.draw_text(x=120, y=216, text='按B键进入管理者模式', origin='bottom')
mouse_pos = gui.draw_text(x=120, y=320, text='鼠标坐标',origin='bottom')    
servo = Servo(Pin((Pin.P16)))

def mouse_move(pos_x, pos_y):
    global x
    global y
    x = pos_x
    y = pos_y
    
def ui_init_user(label_user_cb):
    gui.draw_image(x=0, y=0, w=240, h=270, image='image_servo/root.png')
    info = gui.draw_text(x=10, y=270, font_size=10, text=' { } 的方位是还没有被设置！')
    lavatory = gui.draw_text(x=80, y=20, font_size=10, text='厕所',origin='bottom' ,onclick=lambda : label_user_cb('厕所', info))
    cargo = gui.draw_text(x=50, y=75, font_size=10, text='家用百货',origin='bottom' ,onclick=lambda : label_user_cb('家用百货', info))
    books = gui.draw_text(x=130, y=75, font_size=10, text='书籍',origin='bottom' ,onclick=lambda : label_user_cb('书籍', info))
    exitdoor = gui.draw_text(x=200, y=75, font_size=10, text='出口',origin='bottom' ,onclick=lambda : label_user_cb('出口', info))
    food = gui.draw_text(x=50, y=155, font_size=10, text='休闲食品',origin='bottom' ,onclick=lambda : label_user_cb('休闲食品', info))
    cleaning = gui.draw_text(x=135, y=155, font_size=10, text='家用清洁',origin='bottom' ,onclick=lambda : label_user_cb('家用清洁', info))
    checkstand = gui.draw_text(x=200, y=155, font_size=10, text='收银台',origin='bottom' ,onclick=lambda : label_user_cb('收银台', info))
    donotknow = gui.draw_text(x=45, y=230, font_size=10, text='冲调干杂',origin='bottom' ,onclick=lambda : label_user_cb('冲调干杂', info))
    beverage = gui.draw_text(x=135, y=230, font_size=10, text='酒水饮料',origin='bottom' ,onclick=lambda : label_user_cb('酒水饮料', info))
    entrance = gui.draw_text(x=200, y=240, font_size=10, text='入口',origin='bottom' ,onclick=lambda : label_user_cb('入口', info))

def label_user_cb(label_name: str, info):
    if postition_xy.get(label_name) == None:
        info.config(x=10, y=270, font_size=10, text=f'{label_name} 的方位是还没有被设置！')
    else:
        posx, posy = postition_xy[label_name]
        pos_angle = position_dict[label_name]
        info.config(x=10, y=270, font_size=10, text=f'{label_name} 的方位是 ({posx}, {posy}) 舵机的角度是 {pos_angle}')
    
def ui_init_root(label_root_cb):
    gui.on_mouse_move(mouse_move)
    gui.draw_image(x=0, y=0, w=240, h=270, image='image_servo/root.png')
    lavatory = gui.draw_text(x=80, y=20, font_size=10, text='厕所',origin='bottom' ,onclick=lambda : label_root_cb('厕所', x, y))
    cargo = gui.draw_text(x=50, y=75, font_size=10, text='家用百货',origin='bottom' ,onclick=lambda : label_root_cb('家用百货', x, y))
    books = gui.draw_text(x=130, y=75, font_size=10, text='书籍',origin='bottom' ,onclick=lambda : label_root_cb('书籍', x, y))
    exitdoor = gui.draw_text(x=200, y=75, font_size=10, text='出口',origin='bottom' ,onclick=lambda : label_root_cb('出口', x, y))
    food = gui.draw_text(x=50, y=155, font_size=10, text='休闲食品',origin='bottom' ,onclick=lambda : label_root_cb('休闲食品', x, y))
    cleaning = gui.draw_text(x=135, y=155, font_size=10, text='家用清洁',origin='bottom' ,onclick=lambda : label_root_cb('家用清洁', x, y))
    checkstand = gui.draw_text(x=200, y=155, font_size=10, text='收银台',origin='bottom' ,onclick=lambda : label_root_cb('收银台', x, y))
    donotknow = gui.draw_text(x=45, y=230, font_size=10, text='冲调干杂',origin='bottom' ,onclick=lambda : label_root_cb('冲调干杂', x, y))
    beverage = gui.draw_text(x=135, y=230, font_size=10, text='酒水饮料',origin='bottom' ,onclick=lambda : label_root_cb('酒水饮料', x, y))
    entrance = gui.draw_text(x=200, y=240, font_size=10, text='入口',origin='bottom' ,onclick=lambda : label_root_cb('入口', x, y))
    
def ui_init_config(label_name: str, posx: int, posy: int):
    gui.draw_text(x=5, y=20, font_size=10, text=f'您现在正在设置的是 {label_name} 的方位')
    info = gui.draw_text(x=30, y=200, font_size=20, text=' ')
    gui.draw_image(x=120, y=80, w=50, h=50, image='image_servo/direction-up.png', origin='center', onclick= lambda : solid_down_cb())
    gui.draw_image(x=120, y=180, w=50, h=50, image='image_servo/direction-down.png', origin='center', onclick=lambda : solid_up_cb())
    gui.draw_image(x=170, y=130, w=50, h=50, image='image_servo/direction-right.png', origin='center', onclick=lambda : solid_right_cb())
    gui.draw_image(x=70, y=130, w=50, h=50, image='image_servo/direction-left.png', origin='center', onclick=lambda : solid_left_cb())
    
    gui.draw_image(x=40, y=270, w=50, h=50, image='image_servo/affirm.png', origin='center', onclick=lambda : affirm_cb(label_name, posx, posy, info))
    gui.draw_text(x=10, y=280, font_size=20, text='确认') 
    gui.draw_image(x=120, y=270, w=50, h=50, image='image_servo/refuse.png', origin='center', onclick=lambda : refuse_cb(label_name, info))
    gui.draw_text(x=90, y=280, font_size=20, text='删除')
    gui.draw_image(x=200, y=270, w=50, h=50, image='image_servo/return.png', origin='center', onclick=lambda : return_cb())
    gui.draw_text(x=170, y=280, font_size=20, text='返回')

def label_root_cb(label_name: str, posx: int, posy: int):
    gui.clear()
    ui_init_config(label_name, posx, posy)

def affirm_cb(label_name: str, posx: int, posy: int, info) -> None:
    if position_dict.get(label_name) == None:
        position_dict[label_name] = angle
        postition_xy[label_name] = (posx, posy)
        info.config(x=30, y=200, font_size=20, text='成功标定位置！')
    else:
        position_dict[label_name] = angle
        postition_xy[label_name] = (posx, posy)
        info.config(x=30, y=200, font_size=20, text='重新标定位置！')

def refuse_cb(label_name: str, info) -> None:
    if position_dict.get(label_name) == None:
        info.config(x=30, y=200, font_size=20, color='Red', text='此位置还未被标定！')
    else:
        position_dict.pop(label_name)
        postition_xy.pop(label_name)
        print(f"Successfully deleted {label_name} location!")
        info.config(x=30, y=200, font_size=20, text='成功删除位置！')

def return_cb() -> None:
    print("Return")
    # 返回管理者模式
    gui.clear()
    ui_init_root(label_root_cb)

def solid_down_cb() -> None:
    global angle
    angle -= 5
    if angle <= -90:
        angle = -90
    print(f"The steering gear turns {angle} degrees right")
    servo.write_angle(angle)

def solid_up_cb() -> None:
    global angle
    angle += 5
    if angle >= 90:
        angle = 90
    print(f"The steering gear turns {angle} degrees left")
    servo.write_angle(angle)


def solid_right_cb() -> None:
    global angle
    angle += 5
    if angle >= 90:
        angle = 90
    print(f"The steering gear turns {angle} degrees right")
    servo.write_angle(angle)

def solid_left_cb() -> None:
    global angle
    angle -= 5
    if angle <= -90:
        angle = -90
    print(f"The steering gear turns {angle} degrees left")
    servo.write_angle(angle)

while True:
    if button_a.is_pressed() == True:
        gui.clear()
        ui_init_user(label_user_cb)
    if button_b.is_pressed() == True:
        gui.clear()
        mouse_pos = gui.draw_text(x=120, y=320, text='鼠标坐标',origin='bottom')   
        ui_init_root(label_root_cb)